//
//  TFTurntableView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/3.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFTurntableView: TFtouchesMoved, CAAnimationDelegate {
    //转盘背景
    @objc public let turntableBackgroundImageView = UIImageView()
    @objc public let closeButton = UIButton()
    @objc public let pointer = UIImageView()//指针中心
    @objc public var count:NSInteger = 0
    @objc public var finished:Bool = true //是否已经动画结束  (默认为YES)
    @objc public var isClose:Bool = false
 
    override init(frame: CGRect) {
        super.init(frame: frame)
        //转盘背景
        
        self.turntableBackgroundImageView.image = UIImage.init(named: "HTLive.bundle/turntable")
        self.addSubview(self.turntableBackgroundImageView)
        self.closeButton.setImage(UIImage.init(named: "HTLive.bundle/close_slices"), for: .normal)
       
        self.closeButton.backgroundColor = UIColor(red: 23/255.0, green: 145/255.0, blue: 251/255.0, alpha: 1)
        self.closeButton.addTarget(self, action: #selector(self.closeButtonClict1), for: .touchUpInside)
       
        self.addSubview(closeButton)
        
        self.pointer.image = UIImage.init(named: "HTLive.bundle/interactionPointer")
        self.addSubview(pointer)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //转盘应用
    @objc public func turntableApplicate()

    {
       if(self.isHidden)
        {
           self.isHidden = false
        }

    }
    //开始转盘转盘
    @objc public func startRotationAnimation(_ number:CGFloat)
  
    {

        if(self.isHidden)
        {
            self.isHidden = false;
            
        }
         self.closeButton.isHidden = true;
         self.finished = false;
         self.isClose = false;
         var turnAngle:CGFloat = 360/6//8个奖励分别对应的角度

         let turnsNum:CGFloat = 3//控制圈数

         let perAngle:CGFloat = .pi/180.0
           
         let rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z")
        
           turnAngle =  turnAngle * (7-number);
           let toValue = turnAngle * perAngle + 360 * perAngle * turnsNum
           rotationAnimation.toValue = toValue
           rotationAnimation.duration = 3.0
           rotationAnimation.isCumulative = true
           rotationAnimation.delegate = self
           //由快变慢
        rotationAnimation.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeOut)
        rotationAnimation.fillMode=CAMediaTimingFillMode.forwards
           rotationAnimation.isRemovedOnCompletion = false
           self.turntableBackgroundImageView.layer.add(rotationAnimation, forKey: "rotationAnimation")
        // 转盘结束后调用，显示获得的对应奖励

    }
    //动画结束
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        self.finished = true;
        self.closeButton.isHidden = false
        
        if (self.isClose) {
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5) {
                self.isHidden = true;
            }
         
        
        }
    }
    @objc func closeButtonClict1()
    {
        self.isClose = true
        if (self.finished) {
           self.isHidden = true
        }
    }
    @objc public func closeButtonClict()
    {
        self.isClose = false
        self.isHidden = true
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.turntableBackgroundImageView.frame = CGRect(x: 0, y: 12.5, width: 155, height: 155)

        self.closeButton.frame = CGRect(x: self.frame.size.width-25, y: 0, width: 25, height: 25)
        self.closeButton.layer.cornerRadius =  self.closeButton.bounds.size.width / 2
        self.pointer.bounds = CGRect(x: 0, y: 0, width: 60, height: 72)
        self.pointer.center = self.turntableBackgroundImageView.center
    }

}
